<?php

class lb_system{

	/**
	 * redirect to desired page
	 * 
	 * @access	public
	 * @author Omid Mottaghi
	 * @param 	string	url of page
	 * @param	string	message to show
	 */
	public function redirect($url, $msg){
		global $lb;
		
		$lb['css']	= $lb['template_dir'] . $lb['template'] . '/' . $lb['main_css'];
		$lb['smarty']->tpl_dir('');
		$lb['smarty']->assign ('message'	, $msg);
		$lb['smarty']->assign ('url'		,$url);
		$lb['smarty']->assign ('css', $lb['css']);
		$lb['smarty']->display('redirect.tpl');
		//header('Location: '. $url);
		exit();
	}
	
	/**
	 * returned hashed string (one-way encoding)
	 * 
	 * @access	public
	 * @author Omid Mottaghi
	 * @param 	string	string to be hashed
	 * @return 	string	hashed string
	 */
	public function hash($string){
		$string = md5($string);
		return $string;
	}
	
	/**
	 * finish page proccessing and exit
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	 exit reason
	 */
	public function runaway($why){
		global $lb;
		
		$lb['error'][] = $why;
		$lb['task'] = 'error';
	}
	
	/**
	 * Convert all applicable characters to HTML entities
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	string with applicable html tags
	 * @return	string	html entity string
	 */
	public function htmlentities($string){
		global $lb;
		
		$str = htmlentities($string, ENT_QUOTES, $lb['charset']);
		return $str;
	}
	
	/**
	 * Convert special characters to HTML entities
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param		string	string with special characters
	 * @return	string	filtered string
	 */
	public function htmlspecialchars($string){
		global $lb;
		
		//$str = htmlspecialchars($string, ENT_COMPAT, $lb['charset']);
		$str = str_replace(array('"', '"', '\'', '<', '>')
					     , array('&quot;', '&quot;', '&#039;', '&lt;', '&gt;')
					     , $string);
		return $str;
	}
	
	/**
	 * Strip tags from html or php code
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	html or php code
	 * @param	array	array of allowed tags
	 * @return	string	html code with only allowed tags
	 */
	public function strip_tags($code, $allows){
		$allowed = ''; 
		foreach($allows as $allow){
			 $allowed .= $allow;
		}
		return strip_tags($code, $allowed);
	}
	
	/**
	 * Safe input string for cross site scripting
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	unsafe string
	 * @return	string	safe string
	 */
	public function safe_html($str){
		$xss 	= array(
					'/<(script|applet|embed|frame|frameset|iframe|object|style|html|body|head|meta|title|ilayer|layer|link|id).*>(.*)<\/script>/Ui',
					'/<(.+)\s.*(javascript|vbscript|livescript|mocha|behaviour):.*>/Ui',
					'/<(.+)\s.*(\saction|\sdynsrc|\slowsrc|\sexpression|\sstyle).*>/Ui',
					'/<(.+)(\s.*)?\son.*>/Ui'
					);
		$free_xss = array(
					'\1',
					'<\1>',
					'<\1>',
					'<\1>'
					);
		return preg_replace($xss, $free_xss, $str);
	}
	
	/**
	 * addslashes
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param		string
	 * @return	string
	 */
	public function addslashes($str){
		return addslashes($str);
	}
	
	/**
	 * stripslashes
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param		string
	 * @return	string
	 */
	public function stripslashes($str){
		return stripslashes($str);
	}
	
	/**
	 * spilits search query string 
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	search query
	 * @return	array		dedicated search queries
	 */
	public function search_query($str){
		global $lb;
		$str = urldecode($str);
		
		if(trim($str) == '')
			return false;
		
		$qrs = explode(' ', $str);
		foreach($qrs as $qr){
			if($qr != ''){
				$result[] = array('or', $qr);
			}
		}
		return $result;
	}
	
	/**
	 * escape shell metacharacters
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	string
	 * @return	string	string whithout shell command
	 */
	public function escapeshellcmd($str){
		
		$str = escapeshellcmd($str);
		return $str;
	}
	
	/**
	 * list directories
	 * 
	 * @access	public
	 * @author 	Omid Mottaghi
	 * @param 	string	directory
	 */
	public function list_dir($directory){
		
		if ($handle = opendir($directory)) {
			$list = array();
			
		    /* This is the correct way to loop over the directory. */
		    while (false !== ($file = readdir($handle))) {
		        if(is_dir($directory . $file) && $file!= '.' && $file != '..' && $file != '.svn'){
		        	$list[] = $file;
		        }
		    }
		    closedir($handle);

		    return $list;
		}else{
			return false;
		}
	}
	
	/**
	 * filter array and erease filter array from orginal array
	 * 
	 * @access	public
	 * @author 	Armin Borumand
	 * @param 	array		filter array
	 * @param	array		array
	 * @return	array		filtered array
	 */
	
	public function array_filter($filter, $arr){
	
		foreach($arr as $key=>$val){
			if(!array_search($val, $filter)){
				$result[$key] = $val;
			}
		}
		
		return $result;
	}
	
	/**
	 * return declared classes
	 * 
	 * @access	public
	 * @author 	Armin Borumand
	 * @return	array		 declared classes
	 */
	public function get_declared_classes(){
		return get_declared_classes();
	}

	/**
	 * send a cookie
	 * 
	 * @access	public
	 * @author	Armin Borumand
	 * @param 	string	cookie name
	 * @param	string	cookie value
	 * @return  boolean true on success, false on error
	 */
	public function setcookie($key, $val, $noexpire=true){
		global $lb;
		
		$c_key	= $lb['cookie'] . '[' . $key . ']';
		
		if(	isset($lb['cookie_domain'])
		 && isset($lb['cookie_path'])
		 && isset($lb['cookie_secure']) ) {

			$c_domain = $lb['cookie_domain'];		
			$c_path	  = $lb['cookie_path'];
			$c_secure = $lb['cookie_secure'];
			
		}else{
			$url = explode('/', $lb['webdir']);

			if($url[0] == 'http:'){
				$c_secure = 0;
			}else{
				$c_secure = 1;
			}
			
			$c_domain = $url[2];
			
			$j = 3;
			while($j--){
				array_shift($url);
			}
			
			$c_path = '/' . implode('/', $url);
		}
		
		if($noexpire === true){
			// one year = 31536000 = 60 * 60 * 24 * 365
			$c_expire = time() + 31536000;
		}elseif($noexpire == 'session'){
			$c_expire = false;
		}else{
			$c_expire = -1;
		}	
		
		if(strrpos($c_domain, '.') == false){
			return setcookie($c_key, $val, $c_expire, $c_path);
		}else{
			return setcookie($c_key, $val, $c_expire, $c_path, $c_domain , $c_secure);
		}
	}
	
	/**
	 * Get cookie value
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	cookie key
	 * @return	string	cookie value
	 */
	public function getcookie($key=false){
		global $lb;
		
		if($key === false){
			return $_COOKIE[$lb['cookie']];
		}else{
			if(isset($_COOKIE[$lb['cookie']][$key])){
				return $_COOKIE[$lb['cookie']][$key];
			}else{
				return false;
			}
		}
	}
	
	/**
	 * Generate a html tag
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	tag name
	 * @param	array		tag attributes
	 */
	public function html_tag($name, $params=array(), $content=false){
		$atr = '';
		foreach($params as $key=>$val){
			$atr .= ' ' . $key . '="' . $val . '"';
		}
		$html = '<' . $name . $atr;
		if($content === false){
			$html .= ' />';
		}else{
			$html .= '>' . $content . '</' . $name . '>';
		}
		return $html;
	}
	
	/**
	 * Load js editor
	 *
	 * @access	public
	 * @author	Armin Borumand
	 */
	public function load_editor(){
		global $lb;
		
		$lb['smarty']->assign('load_editor', true);
	}

	/**
	 * create a calendar date format from db timestamp
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	db timestamp
	 */
	public function make_time(&$time){
		global $lb;
		
		$time = $lb['calendar']->date($lb['date_format'], $lb['db']->timestamp($time));
	}	
	
}

?>
